<?php 
class CompanyModel extends BaseModel {
	public function getUser($uname) {
		$data = $this->where(array('company_admin'=>$uname,'is_del'=>0))->find();
		return $data;
	}
	public function getUserbycid($cid) {
		$data = $this->where(array('cid'=>$cid,'is_del'=>0))->find();
		return $data;
	}
	public function add_logintime($aid) {
		$map['login_time'] = time();
		$data = D('Company')->where(array('cid'=>$aid))->save($map);
		if($data){
			return true;	
		}
	}
	public function sign_out(){
		$re = $this->logout();
		return $re;
	}
	public function confirminfo(){
		$cid = $_SESSION['company']['cid'];
		$result = D('company_info')->where('cid = '.$cid)->find();
		if(empty($result)){
			$data['cid'] = $cid;
			D('company_info')->add($data);
		}
	}
	public function getinfo(){
		$cid = $_SESSION['company']['cid'];
		$result = D('company_info')->where('cid = '.$cid)->find();
		return $result;
	}
	public function saveinfo($data){
		$cid = $_SESSION['company']['cid'];
		$map['company_name'] = $data['company_name'];
		$map['updateline'] = time();
		$account_result = D('company')->where('cid = '.$cid)->save($map);
		$data['updateline'] = time();
		//$data['map_x'] = 100000*$data[]
		$info_result = D('company_info')->where('cid = '.$cid)->save($data);
		if($account_result&&$info_result){
			return true;
		}else{
			return false;
		}
	}
	public function update_license($aid){
		$cid = $_SESSION['company']['cid'];
		$data['license'] = $aid;
		$result = D('company_info')->where('cid = '.$cid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	public function status_license(){
		$cid = $_SESSION['company']['cid'];
		$license = D('company_info')->where('cid = '.$cid)->find();
		if($license['license']>0){
			if($license['identify']==1){
				return 1;//已经认证过
			}else{
				return 2;//已经上传，未通过审核
			}
		}else{
			return 3;//未上传
		}
	}
	public function addstore($data){
		$data['cid'] = $_SESSION['company']['cid'];
		$result = D('store')->add($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	public function list_store(){
		$cid = $_SESSION['company']['cid'];
		$map['cid'] = $cid;
		$map['is_del'] = 0;
		$stores = D('store')->where($map)->findpage(20);
		return $stores;
	}
	public function get_store($sid){
		$store = D('store')->where('sid = '.$sid)->find();
		return $store;
	}
	public function edit_store($data){
		$sid = $data['sid'];
		$result = D('store')->where('sid = '.$sid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	public function get_brand(){
		$cid = $_SESSION['company']['cid'];
		$company_info = D('company_info')->where('cid = '.$cid)->find();
		$brands = D('brand')->where('bid IN ('.$company_info['brand'].')')->findpage(20);
		foreach($brands['data'] as $k => &$v){
			$v['brand_ico'] = get_img_url($v['brand_ico']);
		}
		return $brands;
	}
	public function add_brand($data){
		$result = D('brand')->add($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	public function search_brand($key){
		$cid = $_SESSION['company']['cid'];
		$brands = D('brand')->where('brand_name LIKE "%'.$key.'%"')->select();
		$binded = D('company_info')->where('cid = '.$cid)->find();
		if(!empty($binded['brand'])){
			$binded = explode(',',$binded['brand']);
			foreach($brands as $k => $v){
				if(!in_array($v['bid'],$binded)){
					$brands2[] = $v;
				}
			}
		}else{
			$brands2 = $brands;
		}
		foreach($brands2 as $k => &$v){
			$v['brand_ico'] = get_img_url($v['brand_ico']);
		}
		return $brands2;
	}
	public function bind_brand($bid){
		$cid = $_SESSION['company']['cid'];
		$brand = D('company_info')->where('cid = '.$cid)->find();
		if(empty($brand['brand'])){
			$data['brand'] = $bid;
		}else{
			$brands = explode(',',$brand['brand']);
			$brands[count($brands)] = $bid;
			$data['brand'] = implode(',',$brands);
		}
		$result = D('company_info')->where('cid = '.$cid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function edit_pwd($aid,$pwd){
		$data['company_salt'] = rand(10000,99999);
		$data['company_pwd'] = md5(md5($pwd).$data['company_salt']);
		$result = $this->where(array('cid'=>$aid))->save($data);
		return $result;
	}

	public function get_manage_list(){
		$cid = $_SESSION['company']['cid'];
		$manage = D('company_manage')->where('cid = '.$cid.' AND is_del = 0')->select();
		return $manage;
	}
	
	public function get_company_diyinfo(){
		$cid = $_SESSION['company']['cid'];
		$company = D('company_info')->where('cid = '.$cid)->find();
		$diyinfo['style'] = $company['diystyle'];
		$diyinfo['tool'] = explode(',',$company['diytool']);
		$diyinfo['bg'] = get_img_url($company['diybg']);
		$diyinfo['logo'] = get_img_url($company['logo']);
		return $diyinfo;
	}
	
	public function set_company_diystyle($style){
		$cid = $_SESSION['company']['cid'];
		$data['diystyle'] = $style;
		$result = D('company_info')->where('cid = '.$cid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function set_company_diybg($aid){
		$cid = $_SESSION['company']['cid'];
		$data['diybg'] = $aid;
		$result = D('company_info')->where('cid = '.$cid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function set_company_diylogo($aid){
		$cid = $_SESSION['company']['cid'];
		$data['logo'] = $aid;
		$result = D('company_info')->where('cid = '.$cid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function set_company_diytool($toolid){
		$cid = $_SESSION['company']['cid'];
		$info = D('company_info')->where('cid = '.$cid)->find();
		$tool = explode(',',$info['diytool']);
		$tool[count($tool)] = $toolid;
		$data['diytool'] = implode(',',$tool);
		$result = D('company_info')->where('cid = '.$cid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function esc_company_diytool($toolid){
		$cid = $_SESSION['company']['cid'];
		$info = D('company_info')->where('cid = '.$cid)->find();
		$tool = explode(',',$info['diytool']);
		foreach($tool as $k => $v){
			if($v==$toolid){
				unset($tool[$k]);
			}
		}
		$data['diytool'] = implode(',',$tool);
		$result = D('company_info')->where('cid = '.$cid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
}
?>